var form, parentId,index_i_0,parentType,secondType,count=1,projectsGlo,monitorMethod,flag_img_change=0;
var flag = false;
$(function() {
	store.set('pid_arr', '');
	if(store.get('loginName')){
		$("#loginName").text(store.get('loginName'));
		$("#name").val(store.get('loginName'));
	}else{
		$.ajax({
			type:'post',
			url:'/olm/getSession',
			data:'',
			success:function(r){
				var result = r.data;
				if(result){
					store.set('loginName',result.loginName);
					$("#loginName").text(result.loginName);
					$("#name").val(result.loginName);
				}else{
					window.location.href = "/olm/login.html";
				}
			}
		})
	}
	
	layui.use('form', function() {
		form = layui.form;
		//头部项目信息
		getProjectlist();
		form.on('select(gongdian)', function(data) {
			if(count==1){
				secondType = data.elem[data.elem.selectedIndex].getAttribute('type');//2018-12-18--bxf添加
			}
			saveStationId($("select[name=gongdian]").val());
			count++;//作为标记
			parentId = data.value;
			parentType = data.elem[data.elem.selectedIndex].getAttribute('type');
			if(parentId){
				publicAjax('menu/queryMenuByStation', {
	//				employeeId: 'D85A74FE-77AA-425B-ACAF-7E92E05F1D25',
					stationId: parentId
				}, function(data) {
					console.info(data.data);
					$(".side-menu").html("<li class='menu-header'>操作菜单</li>");
					if(data.data && data.data.length > 0) initMenu(data.data, $(".side-menu"));
					$(".side-menu > li").addClass("menu-item");
					/*获取菜单icon随机色*/
					getMathColor();
					
					if(secondType == parentType){
						refreshnavcontent();
						refreshiframe();
					}else{
						//关闭所有标签
						$(".tab-nav-content a").each(function(i, e) {
							if(i != 0) {
								$(e).remove();
							} else {
								$(e).addClass('active');
							}
						});
						$("#layout-main-body iframe").each(function(i, e) {
							if(i != 0) {
								$(e).remove();
							} else {
								$(e).css('display', 'block');
							}
						})
						secondType = parentType;
					}
				})
			}else{
				$(".layout-main-body .body-iframe")[$(".layout-main-body .body-iframe").length-1].contentWindow.location.reload(true);//刷新当前打开页面
			}
	
			
		});
	})
	
	$(document).on('change', "#upImg1", function(event) {
		flag_img_change = 1;
		var $file = $(this),
			fileObj = $file[0],
			windowURL = window.URL || window.webkitURL,
			dataUrl,
			hasSameNameFile = false,
			pictrueName = '',
			dataName = $(this).attr('name');
		if(fileObj && fileObj.files && fileObj.files[0]) {
			file_obj = fileObj.files[0];
			dataUrl = windowURL.createObjectURL(fileObj.files[0]);
			pictrueName = fileObj.files[0].name;
		} else {
			dataUrl = $file.val();
			pictrueName = $file.val();
		}
		if($file.attr('data-id')) {
			$("#img_show").attr('src', dataUrl);
		} else {
			$("#img_show1").attr('src', dataUrl);
		}
	})
	
	// 文档就绪
	//		chu_project_work.Init("projectselect", true, function(d1, d2) {});
	showCurrentDate();
	geteqmType();
	geteqmMANUFACTURER();
	geteqmISUSE();
	geteqmSTATE();
	loadselect();
});

function refreshnavcontent(){
	flag = false;
	if($(".tab-nav-content a").length==1){
		$(".tab-nav-content a").addClass('active');
	}else{
		$(".tab-nav-content a").each(function(i, e) {
			if($(e).hasClass('active')){
				//alert(123);
				var pid = $(e).data("openpid") ;
				var id = $(e).data("openid");
				$(".side-menu li").removeClass("selected_a active");
				$(".side-menu li").each(function(i,e){
					if($(e).data("menuid")==pid){
						$(e).addClass("active");
						$(e).children("ul").addClass("menu-open");
						$(e).find(".menu-item-child li").each(function(i,e1){
							if($(e1).data("menuid")==id){
								$(e1).addClass("selected_a");
								flag = true;
							}
						})
					}
				})
			}else{
				if(i != 0) {
					$(e).remove();
				}
			}
		});
		if(!flag){
			$(".tab-nav-content a:first").addClass('active');
			$("#layout-main-body iframe:first").css('display', 'block');
			$(".tab-nav-content a").each(function(i, e) {
				if(i != 0) {
					$(e).remove();
				}
			})
		}
	}
}

function refreshiframe(){
	if($("#layout-main-body iframe").length==1){
		$("#layout-main-body iframe").css('display', 'block');
	}else{
		var id = $(".tab-nav-content a.content-tab.active").data("openid");
		var iframeid = ""
		$("#layout-main-body iframe").each(function(i, e) {
			if($(e).data('flag')==id && flag==true){
				iframeid = $(e).attr("id");
			}else{
				if(i != 0) {
					$(e).remove();
				}
			}
		});
		document.getElementById(iframeid).contentWindow.location.reload(true);//刷新当前打开页面
	}
}

function getProjectlist(employeeId) {
	if(employeeId) {
		var employeeIds = employeeId;
	} else {
		var employeeIds = '';
	}
	publicAjax('common/listProjectStation', {
//		employeeId: '075C4091-D28C-4076-88BA-54E0629988D4'
	}, onSucc);

	function onSucc(data) {
		$('select[name="xiangmu"]').html('<option value="">请选择项目</option>');
		var pid_arr = [];
		if(!data.data) return;
		var projects = data.data;
        projectsGlo = projects;//存储项目
		if (data.data.length == 1) {
			$('select[name="xiangmu"]').html('');
			if(data.data[0].child.length > 0) {
				if (data.data[0].child.length == 1) {
					parentId = data.data[0].child[0].stationId;//2019.1.1 bxf添加
					$('select[name="gongdian"]').html('');
				} else{
					$('select[name="gongdian"]').html('<option value="" type="">请选择工点</option>');
				}
				$.each(data.data[0].child, function(i, e) {
					var option1 = $("<option>").val(e.stationId).text(e.name).attr('type',e.type);
					$('select[name="gongdian"]').append(option1);
					saveStationId(e.stationId);
				});
			}
		}
		$.each(data.data, function(i, e) {
			pid_arr = pid_arr.concat(e.child);
			var option1 = $("<option>").val(e.projectId).text(e.name).attr('data-child', JSON.stringify(e.child));
			$('select[name="xiangmu"]').append(option1);
			form.render('select');
		});
		store.set('pid_arr', pid_arr);
		form.on('select(xiangmu)', function(data) {
			$("#messages").html('<img src="common/images/ico_message.png" />');
			$('select[name="gongdian"]').html('<option value="" type="">请选择工点</option>');
			if(!data.value) {
				form.render('select');
				return;
			}
			var data = JSON.parse($(data.elem).find('option[value=\'' + data.value + '\']').attr('data-child'));
			if(data.length > 0) {
				if (data.length == 1) {
					parentId = data[0].stationId;//2018.12.20 bxf添加
					$('select[name="gongdian"]').html('');
				} 
				$.each(data, function(i, e) {
					var option1 = $("<option>").val(e.stationId).text(e.name).attr('type',e.type);
					$('select[name="gongdian"]').append(option1);
				});
			}
			form.render('select');
			saveStationId($("select[name=gongdian]").val());
		});
		// window.frames["measuremanage"].window.pid = '1';
	}
}

function geteqmType() { //采集设备类型
	publicAjax('common/getParams', {
		name: 'TYPE'
	}, onSucc);

	function onSucc(data) {
		store.set('device_TYPE', data.data);
	}
}

function geteqmMANUFACTURER() { //采集设备厂商
	publicAjax('common/getParams', {
		name: 'DAE_MANUFACTURER'
	}, onSucc);

	function onSucc(data) {
		store.set('DAE_MANUFACTURER', data.data);
	}
}

function geteqmISUSE() { //设备使用状态
	publicAjax('common/getParams', {
		name: 'IS_USE'
	}, onSucc);

	function onSucc(data) {
		store.set('IS_USE', data.data);
	}
}

function geteqmSTATE() { //设备状态
	publicAjax('common/getParams', {
		name: 'STATE'
	}, onSucc);

	function onSucc(data) {
		store.set('STATE', data.data);
	}
}

function goback() {
	$.ajax({
		type:"post",
		url: publicUrl + 'logout',
		async:true,
		error: function (error){
			console.log(error);
		},
		success: function (data){
			if(data.code == 2000) {
				window.location.href = "login.html"
			} else {
				layer.msg(data.msg,{time: 1000}, function(){});
			}
		}
	});
}

function editpass() {
	layer.open({
		type: 1,
		title: '修改密码',
		area: ['480px', '330px'],
		content: $('#passform'),
		btn: ['确定'],
		success: function(layero, index) {
		},
		yes: function(index, layero) {
			index_i_0 = index;
			submitupdata();
		},
		cancel: function(index, layero) {
			form.val("formTest", {
				"originalPassword": '',
				"newPassword": '',
				"password": ''
			})
			layer.close(index);
		}
	});
}

function editinfo() {
	layer.open({
		type: 1,
		title: '个人信息修改',
		area: ['450px', '650px'],
		content: $('#editpeops'),
		btn: ['确定'],
		success: function(layero, index) {
			publicAjax('getSession', {}, function(r) {
				var result = r.data;
				/*if(data.code == 200) {
					layer.msg('请重新登录！',{time: 1000}, function(){});
					window.location.href = "login.html"
				} else {
					layer.msg(data.msg,{time: 1000}, function(){});
				}*/
				form.val("formInfo", {
					"loginName": result.loginName,
					"userName": result.userName,
					"eIdCard": result.eIdCard,
					"sex": result.sex,
					"phone": result.phone,
					"email": result.email,
					"comment": result.comment
				})
				$("#employeeId").val(result.employeeId);
				if(result.image){
					$("#img_show1").prop('src',"/olm/file/"+result.image);
				}else{
					$("#img_show1").prop('src',"./common/images/employee_default.png");
				}
				
			})
		},
		yes: function(index, layero) {
			index_i_0 = index;
			updateInfo();
		},
		cancel: function(index, layero) {
			form.val("formInfo", {
				"loginName": '',
				"userName": '',
				"eIdCard": '',
				"sex": '',
				"phone": '',
				"email": '',
				"comment": ''
			})
			$("#employeeId").val('');
			$("#img_show1").prop('src',"./common/images/employee_default.png");
			layer.close(index);
		}
	});
}

function updateInfo(){
	var userName = $("input[name=userName]").val();
	if(!userName) {
		layer.msg('请输入用户名！',{time: 1000}, function(){});
		return;
	}
	// 获取页面已有的一个form表单
	var form = document.getElementById("formInfo");
	// 用表单来初始化
	var formData = new FormData(form);
	formData.append("flag",flag_img_change);
	$.ajax({
		type:'post',
		url:'/olm/user/updateEmployeeInfo',
		data:formData,
		contentType:false,
		processData:false,
		success:function(data){
			if(data.code == 2000) {
				layer.msg('修改成功！',{time: 1000}, function(){
					files.getOriginalFilename() = 0;
				});
				layer.closeAll();
			} else {
				layer.msg('修改失败！',{time: 1000}, function(){});
			}
		}
		
	})
}


function openhelp(){
	$(".header-dropdown-menu.right").slideToggle();
	window.open("/olm/filehelp/02-测量主管操作手册.pdf");
}

function addview() {
	$(".header-dropdown-menu.right").slideToggle();
	layer.open({
		type: 2,
		title: '意见反馈',
		area: ['600px', '500px'],
		content: "./suggestion.html",
		btn: ['确定','取消'],
		success: function(layero, index) {
		},
		yes: function(index, layero) {
			submitsuggest(index,layero);
		},
		btn2:function(index, layero){
		},
		cancel: function(index, layero) {
			layer.close(index);
		}
	});
}

function submitsuggest(index,layero){
	var content = layero.find("iframe")[0].contentWindow;
	var layer;
	layui.use('layer',function(){
		layer = layui.layer;
	});
	var module = $.trim(content.$("select[name=module]").val());
	var issueType = $.trim(content.$("select[name=issueType]").val());
	var issueLevel = $.trim(content.$("select[name=issueLevel]").val());
	var issueDescription = $.trim(content.$("#issueDescription").val());
	if(!module){
		layer.msg('请选择模块！');
		return;
	}
	if(!issueType){
		layer.msg('请选择类型！');
		return;
	}
	if(!issueLevel){
		layer.msg('请选择等级！');
		return;
	}
	if(!issueDescription){
		layer.msg('清输入意见需求！');
		return;
	}
	var data = content.$("#viewformdata").serialize();
	publicAjax('sysIssue/saveSysIssue', data, function(data) {
		if(data.code == 2000) {
			layer.msg('反馈成功，谢谢您的宝贵意见！',{time: 2000}, function(){
				layer.close(index);
			});
		} else {
			layer.msg(data.msg,{time: 1000}, function(){});
		}
	})
	
}


function submitupdata() {
	if(!$("input[name='originalPassword']").val()) {
		layer.msg('请输入原密码！',{time: 1000}, function(){});
		return;
	}
	if(!$("input[name='newPassword']").val()) {
		layer.msg('请输入新密码！',{time: 1000}, function(){});
		return;
	}
	if(!$("input[name='password']").val()) {
		layer.msg('请输入确认密码！',{time: 1000}, function(){});
		return;
	}
	if($("input[name='newPassword']").val() != $("input[name='password']").val()) {
		layer.msg('新密码与确认密码不相同！',{time: 1000}, function(){});
		return;
	}
	layer.close(index_i_0);
	var data = {
		originalPassword: hex_md5($("input[name='originalPassword']").val()),
		newPassword: hex_md5($("input[name='newPassword']").val())
	};
	publicAjax('changePassword', data, function(data) {
		if(data.code == 2000) {
			layer.msg('请重新登录！',{time: 1000}, function(){});
			window.location.href = "login.html"
		} else {
			layer.msg(data.msg,{time: 1000}, function(){});
		}
	})
}

function saveStationId(id){
	$.ajax({
		type:'post',
		url:'/olm/saveStationId',
		data:{"id":id},
		success:function(r){
			
		}
	})
}

function loadselect(){
	publicAjax('common/getParams',{
		name: 'MODULE'
	},function(data){
	$.each(data.data, function(i, e) {
		var option = '<option value="' + e.code + '">' + e.value + '</option>';
		$("select[name='module']").append(option);
	});
	});
	publicAjax('common/getParams',{
		name: 'ISSUE_TYPE'
	},function(data){
	$.each(data.data, function(i, e) {
		var option = '<option value="' + e.code + '">' + e.value + '</option>';
		$("select[name='issueType']").append(option);
	});
	});
	publicAjax('common/getParams',{
		name: 'ISSUE_LEVEL'
	},function(data){
	$.each(data.data, function(i, e) {
		var option = '<option value="' + e.code + '">' + e.value + '</option>';
		$("select[name='issueLevel']").append(option);
	});
	
});
	layui.use('form',function(){
		layui.form.render("select");
	})	
	
}
